#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = 'hhuua'

import logging
from orm import Model, TextField, IntegerField, StringField, select, execute
from db_pool import get_mysql_pool
logging.basicConfig(level=logging.ERROR)


class ArticleInfoModel(Model):
    __table__ = 'article'

    url = StringField()
    title = StringField()
    pic = StringField()
    update_time = StringField()
    content = StringField()
    pid = IntegerField()


    @classmethod
    async def save_with_dic(cls, dic):

        new_article = ArticleInfoModel()

        new_article.url = dic['url'].split('/')[-1]
        new_article.title = dic['title'] or ''
        new_article.pic = dic['thumb_nail_2x'] or ''
        new_article.update_time = dic['update_time'] or ''
        new_article.content = dic['content'] or ''
        new_article.pid = dic['media_id'] or 0

        await new_article.save()


class WriteInfoModel(Model):
    __table__ = 'write'

    id = IntegerField()
    name = StringField()
    pic = StringField()
    description = TextField()
    last_time = IntegerField()

    @classmethod
    async def save_with_dic(cls, dic, last_time):
        new_write = WriteInfoModel()

        new_write.id = dic['mid'] or 0
        new_write.name = dic['name'] or ''
        new_write.pic = dic['icon'] or ''
        new_write.description = dic['intro'] or ''
        new_write.last_time = last_time or ''

        await new_write.save()

    @classmethod
    async def update_time(cls, dic):
        sql = "update `write` set `last_time` = ? where `id` = ?"
        await execute(get_mysql_pool(), sql=sql, args=[dic['update_time'] or '', dic['media_id'] or ''])


class VideoInfoModel(Model):
    __table__ = 'video'

    url = StringField(primary_key=True)
    title = StringField()
    pic = StringField()
    update_time = StringField()
    content = TextField()
    pid = IntegerField()
    time_long = IntegerField()


